package com.runjian.demo.config;

import com.aliyun.oss.ClientConfiguration;
import com.aliyun.oss.OSSClient;
import com.aliyun.oss.common.auth.DefaultCredentialProvider;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;

@Configuration
public class OssBootConfiguration implements InitializingBean {

    @Value("${aliyun.accessKey}")
    private String accessKeyId;
    @Value("${aliyun.secretKey}")
    private String accessKeySecret;
    @Value("${aliyun.oss.endpoint}")
    private String endpoint;
    @Value("${aliyun.oss.bucketName}")
    private String bucketName;
    @Value("${aliyun.oss.staticDomain}")
    private String staticDomain;

    private String aliyunBucket = "rjzhsq-app";
    private String aliyunEndpoint = "oss-accelerate.aliyuncs.com";
    private String aliyunStaticDomain = "https://staticoss.runjian.com";

    private static OssMeta META;
    private static OSSClient CLIENT;

    @Override
    public void afterPropertiesSet() throws Exception {
        META = new OssMeta(endpoint, bucketName, staticDomain);
        CLIENT = new OSSClient(endpoint,
                new DefaultCredentialProvider(accessKeyId, accessKeySecret),
                new ClientConfiguration());
    }

    public static OSSClient ossClient() {
        return CLIENT;
    }

    public static OssMeta ossMeta() {
        return META;
    }

    /**
     * @return the aliyunBucket
     */
    public String getAliyunBucket() {
        return aliyunBucket;
    }

    /**
     * @return the aliyunEndpoint
     */
    public String getAliyunEndpoint() {
        return aliyunEndpoint;
    }

    /**
     * @return the aliyunStaticDomain
     */
    public String getAliyunStaticDomain() {
        return aliyunStaticDomain;
    }


}
